package phone.qyg.com.qyg.adapter;

import android.content.Context;

import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.base.ItemViewDelegate;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.utils.CommonUtil;
import com.app.utils.OptionImageUtils;

import java.text.MessageFormat;
import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Activity;
import phone.qyg.com.qyg.model.Base;
import phone.qyg.com.qyg.model.Course;
import phone.qyg.com.qyg.model.Venues;

/**
 * 作者：lblbh on 2017/12/5 17:07
 * 邮箱：lanbuhan@163.com
 * 功能：全局搜索结果----适配器
 */
public class FindAllAdapter extends MultiItemTypeAdapter<Object> {

    String PERSON_COUNT = "相关结果<font color='#01aefd'>{0}</font>个";

    int tenPadding;

    public FindAllAdapter(Context context, List<Object> datas) {
        super(context, datas);
        tenPadding = CommonUtil.dip2px(QXApplication.getContext(),10);
        addItemViewDelegate(new ActAdapter());
        addItemViewDelegate(new VenuesAdapter());
        addItemViewDelegate(new CourseAdapter());
        addItemViewDelegate(new BaseHeadAdapter());
        addItemViewDelegate(new BaseFootAdapter());
    }

    public class ActAdapter implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.home_item_act;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return (item instanceof Activity);
        }

        @Override
        public void convert(ViewHolder holder, Object model, int position) {
            //活动
            Activity act = (Activity) model;
            holder.setText(R.id.item_title, act.title);
            holder.setText(R.id.item_address, act.startTime);
            holder.setText(R.id.item_map, act.address);
            holder.setText(R.id.item_state, String.valueOf(act.statusName));
            holder.setTextColor(R.id.item_state, act.color);
            holder.setImage(R.id.item_img,act.fixImgUrl, OptionImageUtils.getRectRatioAct());
            holder.setPadding(R.id.item_layout,tenPadding,0,tenPadding,0);
        }
    }

    public class VenuesAdapter implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_venue;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return (item instanceof Venues);
        }

        @Override
        public void convert(ViewHolder holder, Object obj, int position) {
            //场馆
            Venues model = (Venues) obj;
            holder.setText(R.id.item_title,model.title);
            holder.setText(R.id.item_distance,String.valueOf(model.distance));
            holder.setText(R.id.item_map,model.address);
            holder.setImage(R.id.item_img,model.fixImgUrl, OptionImageUtils.getRectRatio43());
            holder.setStar(R.id.rating_bar,model.star);
            holder.setPadding(R.id.item_layout,tenPadding,0,tenPadding,0);
        }
    }

    public class CourseAdapter implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_course_list;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return (item instanceof Course);
        }

        @Override
        public void convert(ViewHolder holder, Object obj, int position) {
            //课程
            Course model = (Course) obj;
            holder.setText(R.id.item_title,model.title);
            holder.setText(R.id.item_address,model.startTime);
            holder.setText(R.id.item_state,String.valueOf(model.regCount));
            holder.setImage(R.id.item_img,model.fixImgUrl, OptionImageUtils.getRectRatioAct());
            holder.setTextHTMLTwo(R.id.item_summary,model.courseDesc);
            holder.setPadding(R.id.item_layout,tenPadding,0,tenPadding,0);
        }
    }

    public class BaseHeadAdapter implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.find_item_head;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return ((item instanceof Base) && (((Base)item).type == 1));
        }

        @Override
        public void convert(ViewHolder holder, Object obj, int position) {
            //头部
            Base model = (Base) obj;
            holder.setText(R.id.item_title,model.title);
            holder.setTextHTMLTwo(R.id.item_cout, MessageFormat.format(
                        PERSON_COUNT,
                        model.id));
        }
    }

    public class BaseFootAdapter implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.find_item_foot;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return ((item instanceof Base) && (((Base)item).type == 2));
        }

        @Override
        public void convert(ViewHolder holder, Object obj, int position) {
            //尾部
            Base model = (Base) obj;
            holder.setText(R.id.item_title,model.title);
        }
    }
}
